<?php 
	class AdminDthink_Model_Provider extends Zend_Db_Table_Abstract
	{
		protected $_name = 'provider';
		protected $_primary = 'provider_id';
		protected $_db;
		
		public function __construct()
		{
			$this->_db = Zend_Registry::get('db');
		}
		
		public function listProvider()
		{
			$sql = $this->_db->select()
							 ->from($this->_name)
							 ->order('provider_id DESC');
			$query = $this->_db->fetchAll($sql);
			return $query;
		}
		
		public function listVoucher()
		{
			$sql = $this->_db->select()
							->from($this->_name)
                            ->where('status = 1')
							->order('provider_name ASC');
			$query = $this->_db->fetchAll($sql);
			return $query;
		}
        
        public function getProviderNameById($provider_id)
        {
            $sql = $this->_db->select('provider_name')
                            ->from($this->_name)
                            ->where('provider_id = ?', $provider_id);
            $result = $this->_db->fetchRow($sql);
            return $result['provider_name'];
        }
		
		public function addProvider($provider_name, $provider_icon = null, $provider_link)
		{
			$data = array(
				'provider_name' => $provider_name,
				'provider_icon' => $provider_icon,
				'provider_link' => $provider_link,
                'status'        => 1,
			);
			$result = $this->_db->insert($this->_name, $data);
			if($result)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		public function editProvider($provider_id, $provider_name, $provider_icon, $provider_link, $status)
		{
			$data = array(
				'provider_name' => $provider_name,
				'provider_icon' => $provider_icon,
				'provider_link' => $provider_link,
                'status'        => $status,
			);
			$where = 'provider_id = ' . $provider_id;
			$result = $this->_db->update($this->_name, $data, $where);
			if($result)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		public function editProviderNotImage($provider_id, $provider_name, $provider_link, $status)
		{
			$data = array(
				'provider_name' => $provider_name,
				'provider_link' => $provider_link,
                'status'        => $status,
			);
			$where = 'provider_id = ' . $provider_id;
			$result = $this->_db->update($this->_name, $data, $where);
			if($result)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		public function deleteProvider($provider_id)
		{
			$where = 'provider_id = ' . $provider_id;
			$query = $this->_db->select()
                            ->from('product')
                            ->where($where);
            $row = $this->_db->fetchAll($query);
            if(count($row) > 0)
            {
                foreach($row as $value)
    			{
                    if (is_file(PUBLIC_PATH . '/files/product/' . $value['product_image']))
        			{
        				unlink(PUBLIC_PATH . '/files/product/' . $value['product_image']);
        			}
    			}
                $this->_db->delete('product', $where);
                $sql = $this->_db->select()
    							 ->from($this->_name)
    							 ->where($where);
    			$row = $this->_db->fetchRow($sql);
    			if (is_file(PUBLIC_PATH . '/files/provider/' . $row['provider_icon']))
    			{
    				unlink(PUBLIC_PATH . '/files/provider/' . $row['provider_icon']);
    			}
    			$result = $this->_db->delete($this->_name, $where);
    			if($result)
    			{
    				return true;
    			}
    			else
    			{
    				return false;
    			}
            }
            else
            {
                $sql = $this->_db->select()
    							 ->from($this->_name)
    							 ->where($where);
    			$row = $this->_db->fetchRow($sql);
    			if (is_file(PUBLIC_PATH . '/files/provider/' . $row['provider_icon']))
    			{
    				unlink(PUBLIC_PATH . '/files/provider/' . $row['provider_icon']);
    			}
    			$result = $this->_db->delete($this->_name, $where);
    			if($result)
    			{
    				return true;
    			}
    			else
    			{
    				return false;
    			}
            }
		}
		
		public function deleteProviders($listId)
		{
			foreach($listId as $value)
			{
				$where = 'provider_id = ' . $value;
				$sql = $this->_db->select()
								 ->from('product')
								 ->where($where);
				$row = $this->_db->fetchAll($sql);
				foreach($row as $key)
				{
					if (is_file(PUBLIC_PATH . '/files/product/' . $key['product_image']))
					{
						unlink(PUBLIC_PATH . '/files/product/' . $key['product_image']);
					}
				}
				$this->_db->delete('product', $where);
				
				$query = $this->_db->select()
								   ->from($this->_name)
								   ->where($where);
				$rowProvider = $this->_db->fetchRow($query);
				if (is_file(PUBLIC_PATH . '/files/provider/' . $rowProvider['provider_icon']))
				{
					unlink(PUBLIC_PATH . '/files/provider/' . $rowProvider['provider_icon']);
				}
				$result = $this->_db->delete($this->_name, $where);
			}
		}
		
		public function getProviderById($provider_id)
		{
			$sql = $this->_db->select()
							 ->from($this->_name)
							 ->where('provider_id = ?', $provider_id);
			$row = $this->_db->fetchRow($sql);
			return $row;
		}
		
		public function checkProvider($provider_name, $provider_id = '')
		{
            if($provider_id != '')
            {
                $sql = $this->_db->select()
    							 ->from($this->_name)
    							 ->where('provider_name = ? AND provider_id = ?', $provider_name, $provider_id);
    			$result = $this->_db->fetchAll($sql);
    			return count($result);
            }
            else
            {
                $sql = $this->_db->select()
    							 ->from($this->_name)
    							 ->where('provider_name = ?', $provider_name);
    			$result = $this->_db->fetchAll($sql);
    			return count($result);
            }
		}
		
		public function searchProvider($keyword)
		{
			$where = "MATCH(provider_name, provider_link) AGAINST('" . $keyword . "' IN NATURAL LANGUAGE MODE)";
			$sql = $this->_db->select()
							 ->from($this->_name)
							 ->where($where);
			$result = $this->_db->fetchAll($sql);
			return $result;
		}
	}
